package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.UUIDUtil;
import com.macro.mall.mapper.admin.BasicUserPortraitMapper;
import com.macro.mall.model.BasicUserPortraitParam;
import com.macro.mall.model.admin.BasicUserPortrait;
import com.macro.mall.query.BasicUserPortraitQuery;
import com.macro.mall.service.SysUserPortraitService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserPortraitServiceImpl implements SysUserPortraitService {

    @Autowired
    private BasicUserPortraitMapper userPortraitMapper;


    @Override
    public CommonResult edit(BasicUserPortraitParam param) {
        if (param.getId() == null || param.getId().equals("") || param.getId().length() <= 0) {
            BasicUserPortrait userPortrait = new BasicUserPortrait();
            BeanUtils.copyProperties(param, userPortrait);
            userPortrait.setId(UUIDUtil.getUUid());
            userPortrait.setCreationTime(new Date());
            userPortrait.setUpdateTime(new Date());
            int row = userPortraitMapper.insertSelective(userPortrait);
            return result(row, "添加失败");
        }
        BasicUserPortrait userPortrait = new BasicUserPortrait();
        BeanUtils.copyProperties(param, userPortrait);
        userPortrait.setUpdateTime(new Date());
        int row = userPortraitMapper.updateByPrimaryKeySelective(userPortrait);
        return result(row, "修改失败");

    }

    @Override
    public CommonResult getList(BasicUserPortraitParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        BasicUserPortraitQuery query = new BasicUserPortraitQuery();
        BeanUtils.copyProperties(param, query);
        List<BasicUserPortrait> userPortraitList = userPortraitMapper.selectList(query);
        PageInfo<BasicUserPortrait> pageInfo = new PageInfo<>(userPortraitList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }

    @Override
    public CommonResult delete(String id) {
        int row = userPortraitMapper.deleteByPrimaryKey(id);
        return result(row, "删除失败");
    }

    @Override
    public CommonResult getOne(String id) {
        BasicUserPortrait userPortrait = userPortraitMapper.selectByPrimaryKey(id);
        return CommonResult.success(userPortrait);
    }


    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }
}
